package webdev.danagtravel;

import java.util.ArrayList;

import webdev.common.Common;
import webdev.database.SQLQuery;
import webdev.dto.UserDTO;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import dev.danang.R;

public class LoginActivity extends Activity implements OnClickListener {

	private TextView mHeader;
	private ImageView mBack;
	private EditText mTxtUserName;
	private EditText mTxtPassword;
	private Button mLogin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sign_in);
		mHeader = (TextView) findViewById(R.id.text_view_header_title);
		mBack = (ImageView) findViewById(R.id.img_back);
		mTxtUserName = (EditText) findViewById(R.id.txt_email);
		mTxtPassword = (EditText) findViewById(R.id.txt_password);
		mLogin = (Button) findViewById(R.id.btn_login);
		
		setValue();
		setListener();
		mTxtUserName.setText("a");
		mTxtPassword.setText("a");
	}

	

	private void setValue() {
		mHeader.setText("Đăng Nhập");
	}

	private void setListener() {
		mBack.setOnClickListener(this);
		mLogin.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		Intent i;
		switch (v.getId()) {
		case R.id.btn_login:
			if (login()) {
				i = new Intent(LoginActivity.this, MainActivity.class);
				startActivity(i);
			}
			break;
		case R.id.img_back:
			i = new Intent(LoginActivity.this, WellcomeActivity.class);
			startActivity(i);
			finish();
			break;
		default:
			break;
		}
	}

	private boolean login() {
		boolean isLoginOk = false;

		String userName = mTxtUserName.getText().toString();
		String password = mTxtPassword.getText().toString();

		if ("".equals(userName)) {
			Toast.makeText(this, "Nhập tên tài khoản", Toast.LENGTH_SHORT).show();
		}
		if ("".equals(password)) {
			Toast.makeText(this, "Nhập mật khẩu", Toast.LENGTH_SHORT).show();
		}

		boolean isHaveThisAccount = false;
		SQLQuery db = new SQLQuery(this);
		ArrayList<UserDTO> listUser = db.getAllUser();
		int size = listUser.size();
		String currentUser,  currentPassword;
		for (int i = 0; i < size; i ++) {
			currentUser = listUser.get(i).getmUserName();
			currentPassword = listUser.get(i).getmPassword();
			if (userName.equals(currentUser) && password.equals(currentPassword)) {
				isHaveThisAccount = true;
				Common.currentUser = new UserDTO(currentUser, currentPassword);
				break;
			}
		}
		if (isHaveThisAccount) {
			isLoginOk = true;
		} else {
			Toast.makeText(this, "Tên tài khoản hoặc mật khẩu không đúng", Toast.LENGTH_SHORT).show();
		}
		
		//===============
//		SQLQuery db = new SQLQuery(this);
//		ArrayList<DiaDiemDTO> diadiems = db.getAllDiaDiem(SQLQuery.TYPE_HOTEL);
//		for (int i = 0; i < diadiems.size(); i++) {
//			Log.i("TCT", bookings.get(i).getNamePlace());
//		}
		//===============

		return isLoginOk;
	}

	public void onBackPressed() {
        Intent setIntent = new Intent(LoginActivity.this, WellcomeActivity.class);
        startActivity(setIntent);
        LoginActivity.this.finish();
        return;
    }

}
